<template>
  <div class="app-container">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="参数名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button
          type="primary"
          @click="addOrUpdateHandle()"
        >新增</el-button>
        <el-button
          type="danger"
          @click="deleteHandle()"
          :disabled="dataListSelections.length <= 0"
        >批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;"
    >
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column prop="id" header-align="center" align="center" label="id"></el-table-column>
      <el-table-column prop="name" header-align="center" align="center" label="场次名称"></el-table-column>
      <el-table-column prop="startTime" header-align="center" align="center" label="每日开始时间">
        <template slot-scope="scope">
          {{ renderTime(scope.row.startTime)}}
        </template>
      </el-table-column>
      <el-table-column prop="endTime" header-align="center" align="center" label="每日结束时间">
        <template slot-scope="scope">
          {{ renderTime(scope.row.endTime)}}
        </template>
      </el-table-column>
      <el-table-column prop="status" header-align="center" align="center" label="启用状态">
        <template slot-scope="scope">
          <i style="font-size: 20px; margin-top: 5px" class="el-icon-success" v-if="scope.row.status === 1"></i>
          <i style="font-size: 20px; margin-top: 5px" class="el-icon-circle-close" v-else></i>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" header-align="center" align="center" label="创建时间">
        <template slot-scope="scope">
          {{ renderTime(scope.row.createTime)}}
        </template>
      </el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="240" label="操作">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="relationProduct(scope.row.id)">关联商品</el-button>
          <el-button type="success" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button type="danger" size="small" @click="deleteHandle(scope.row.name, scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="margin-top: 10px"
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <div>
      <el-dialog
        append-to-body
        :close-on-click-modal="false"
        :visible.sync="visible"
        title="关联秒杀商品"
        width="80%"
      >
        <seckillsku-relation ref="seckillskuRelation" :sessionId="currentId"></seckillsku-relation>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import AddOrUpdate from "./seckillsession-add-or-update";
import SeckillskuRelation from "./seckillskurelation";
import { list, delMultiple } from '@/api/coupon/seckill-session'

export default {
  data() {
    return {
      dataForm: {
        key: null
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      visible: false,
      currentId: null
    };
  },
  components: {
    AddOrUpdate,
    SeckillskuRelation
  },
  created() {
    this.getDataList();
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      list(
        {
          'page': this.pageIndex,
          'limit': this.pageSize,
          'key': this.dataForm.key
        })
        .then(({ data }) => {
          this.dataList = data.list;
          this.totalPage = data.totalCount;
          this.dataListLoading = false;
      });
    },
    getRealtionDataList() {},
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },
    relationProduct(id) {
      this.visible = true;
      this.currentId = String(id);
      this.$nextTick(() => {
        this.$refs.seckillskuRelation.getDataList();
      });
    },
    // 删除
    deleteHandle(name, id) {
      const names = []
      const ids = []
      if (id) {
        ids.push(id)
        names.push(' 【' + name + '】 ')
      } else {
        this.dataListSelections.map(item => {
          ids.push(item.id)
          names.push(' 【' + item.name + '】 ')
          return item.id
        })
      }
      // const ids = id ? [id] :
      this.$confirm(`确定对 ${names.join(',')} 进行${id ? '删除' : '批量删除'}操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        console.log(ids)
        delMultiple(ids)
        .then(() => {
            this.$message({
              message: "删除成功",
              type: "success"
            });
            this.getDataList();
        }).catch(()=>{})
      });
    },
    renderTime(date) {
      const dateee = new Date(date).toJSON();
      return new Date(+new Date(dateee) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '')
    }
  }
};
</script>
